package rpc.client;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import rpc.StudentNameInterface;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;

public class StudentNameClient {

    private static byte[] getIpBytes(String ipV4) {
        String[] ipStr = ipV4.split("\\.");
        byte[] ipBuf = new byte[4];
        for (int i = 0; i < 4; i++) {
            ipBuf[i] = (byte) (Integer.parseInt(ipStr[i]) & 0xff);
        }
        return ipBuf;
    }

    public static void main(String[] args) throws IOException {
        InetAddress addr = InetAddress.getByAddress(getIpBytes("127.0.0.1"));
        StudentNameInterface studentNameClient = RPC.getProxy(StudentNameInterface.class,
                StudentNameInterface.versionID,
                new InetSocketAddress(addr, 12345),
                new Configuration()
        );
        String name1 = studentNameClient.findName(1234);
        System.out.println("name1=" + name1);
        String name2 = studentNameClient.findName(2234);
        System.out.println("name2=" + name2);
        String name3 = studentNameClient.findName(3234);
        System.out.println("name3=" + name3);
    }
}
